package com.ex.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ex.dto.GoodsDto;
import com.ex.mapper.GoodsMapper;
import com.ex.pojo.Contain;
import com.ex.pojo.Goods;
import com.ex.pojo.GoodsContain;
import com.ex.service.CategoryService;
import com.ex.service.ContainService;
import com.ex.service.GoodsContainService;
import com.ex.service.GoodsService;
import com.ex.vo.GoodsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 * discrb:
 *
 * @Author CD
 * @Date2025/6/6 21:30
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    /**
     * 条件查询商品
     *
     * @param dto
     * @return
     */
    @Autowired
    private GoodsContainService goodsContainService;
    @Autowired
    private ContainService containService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public List<GoodsVo> findGoodsList(GoodsDto dto) {
        //判断传来的查询参数是否为空，商品名和成分每次智能查询一个还有分类
        if (dto.getName() != null) {
            //返回集合
            ArrayList<GoodsVo> goodList2 = new ArrayList<>();
            //根据商品名进行查询
            List<Goods> goodsList1 = this.lambdaQuery().like( Goods::getName, dto.getName())
                    .eq(Goods::getStatus, 1)
                    .list();
            //查询商品的成分
            goodsList1.forEach(goodsName -> {
                List<GoodsContain> goodsContainList = goodsContainService.lambdaQuery()
                        .eq(GoodsContain::getGoodId, goodsName.getContain())
                        .list();
                //商品返回对象
                GoodsVo goodsVo = new GoodsVo();
                //单个商品成分集合
                List<String> containList = new ArrayList<>();
                //根据查询到的商品成分id，查询包含的成分
                goodsContainList.forEach(contain -> {
                    List<Contain> containList1 = containService.lambdaQuery()
                            .eq(Contain::getId, contain.getContainId())
                            .list();
                    //获取成分集合的成分名
                    containList1.forEach(contain1 -> {
                        String name = contain1.getName();
                        goodsVo.setName(name);
                        containList.add(name);
                    });
                });
                //复制goods
                BeanUtils.copyProperties(goodsName, goodsVo);
                goodsVo.setContain(containList);
                goodList2.add(goodsVo);
            });
            return goodList2;
        } else if (dto.getContain() != null) {
            ArrayList<GoodsVo> goodList2 = new ArrayList<>();
            //查询所有商品的成分
            List<Goods> goodsList2 = this.lambdaQuery()
                    .eq(Goods::getStatus, 1)
                    .list();
            //查询商品包含的成分
            goodsList2.forEach(goods -> {
                List<GoodsContain> goodsContainList = goodsContainService.lambdaQuery()
                        .eq(GoodsContain::getGoodId, goods.getContain())
                        .list();
                //商品返回对象
                GoodsVo goodsVo = new GoodsVo();
                //单个商品成分集合
                List<String> containList = new ArrayList<>();
                //根据查询到的商品成分id，查询包含的成分
                goodsContainList.forEach(contain -> {
                    List<Contain> containList1 = containService.lambdaQuery()
                            .eq(Contain::getId, contain.getContainId())
                            .list();
                    //获取成分集合的成分名
                    containList1.forEach(contain1 -> {
                        String name = contain1.getName();
                        goodsVo.setName(name);
                        containList.add(name);
                    });
                });
                //判断集合中的成分是否有dto的成分，有的话添加到返回集合中
                boolean b = containList.contains(dto.getContain());
                if (b) {
                    //复制goods
                    BeanUtils.copyProperties(goods, goodsVo);
                    goodsVo.setContain(containList);
                    goodList2.add(goodsVo);
                }
            });
            return goodList2;
        } else if (dto.getCategory()!=null) {
            //返回集合
            ArrayList<GoodsVo> goodList2 = new ArrayList<>();
            //根据商品分类
            List<Goods> goodsList = this.lambdaQuery()
                    .eq(Goods::getCategory, dto.getCategory())
                    .eq(Goods::getStatus, 1)
                    .list();
            //查询商品的成分
            goodsList.forEach(goodsName -> {
                List<GoodsContain> goodsContainList = goodsContainService.lambdaQuery()
                        .eq(GoodsContain::getGoodId, goodsName.getContain())
                        .list();
                //商品返回对象
                GoodsVo goodsVo = new GoodsVo();
                //单个商品成分集合
                List<String> containList = new ArrayList<>();
                //根据查询到的商品成分id，查询包含的成分
                goodsContainList.forEach(contain -> {
                    List<Contain> containList1 = containService.lambdaQuery()
                            .eq(Contain::getId, contain.getContainId())
                            .list();
                    //获取成分集合的成分名
                    containList1.forEach(contain1 -> {
                        String name = contain1.getName();
                        goodsVo.setName(name);
                        containList.add(name);
                    });
                });
                //复制goods
                BeanUtils.copyProperties(goodsName, goodsVo);
                goodsVo.setContain(containList);
                goodList2.add(goodsVo);
            });
            return goodList2;
        }else {
            //查询全部
            //返回集合
            ArrayList<GoodsVo> goodList2 = new ArrayList<>();
            //根据商品分类
            List<Goods> goodsList = this.lambdaQuery()
                    .eq(Goods::getStatus, 1)
                    .list();
            //查询商品的成分
            goodsList.forEach(goodsName -> {
                List<GoodsContain> goodsContainList = goodsContainService.lambdaQuery()
                        .eq(GoodsContain::getGoodId, goodsName.getContain())
                        .list();
                //商品返回对象
                GoodsVo goodsVo = new GoodsVo();
                //单个商品成分集合
                List<String> containList = new ArrayList<>();
                //根据查询到的商品成分id，查询包含的成分
                goodsContainList.forEach(contain -> {
                    List<Contain> containList1 = containService.lambdaQuery()
                            .eq(Contain::getId, contain.getContainId())
                            .list();
                    //获取成分集合的成分名
                    containList1.forEach(contain1 -> {
                        String name = contain1.getName();
                        goodsVo.setName(name);
                        containList.add(name);
                    });
                });
                //复制goods
                BeanUtils.copyProperties(goodsName, goodsVo);
                goodsVo.setContain(containList);
                goodList2.add(goodsVo);
            });
            return goodList2;
        }
    }
}
